package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.foura;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.RoleFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserRoleReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RoleInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserRoleInfoResVO;
import com.aliyun.gts.bpaas.framework.api.rest.dto.ResultResponse;
import com.aliyun.gts.delivery.enterprise.sdk.rbac.in.AccountPermissionQuery;
import com.aliyun.gts.delivery.enterprise.sdk.rbac.in.AccountRoleQuery;
import com.aliyun.gts.delivery.enterprise.sdk.rbac.out.Permission;
import com.aliyun.gts.delivery.enterprise.sdk.rbac.out.Role;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.WaterRbacService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SessionUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import org.flowable.engine.common.impl.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: YeTaoRui
 * @Description:
 * @Date: Create in 13:14 2021/4/29
 * @Modify by:
 */
@Configuration
@ConditionalOnProperty(prefix = "xxl.sso",name = "enable",havingValue = "true")
@Service
public class WaterRbacServiceImpl implements WaterRbacService {

    @Autowired
    RoleFeignService roleFeignService;

    /**
     * 根据角色code获取角色
     *
     * @param s
     * @return
     */
    @Override
    public ResultResponse<Role> getRoleByCode(String s) {

        ResultResponse<List<Role>> roleList = this.getRoleList();
        if (CollectionUtil.isNotEmpty(roleList.getData())){
            List<Role> data = roleList.getData();
            for (Role datum : data) {
                if (datum.getCode().equalsIgnoreCase(s)) {
                    return ResultResponse.succResult(datum);
                }
            }
        }
        return ResultResponse.notFound("不存在的角色");
    }

    @Override
    public ResultResponse<Permission> getPermissionByCode(String s) {
        return null;
    }

    @Override
    public ResultResponse<Permission> getPermissionByCode(String s, String s1) {
        return null;
    }

    @Override
    public ResultResponse<List<Permission>> queryPermissionByRoleCode(String s) {
        return null;
    }

    @Override
    public ResultResponse<List<Permission>> queryPermissionByRoleCode(String s, String s1) {
        return null;
    }

    /**
     * 获取用户角色列表
     *
     * @param accountRoleQuery
     * @return
     */
    @Override
    public ResultResponse<List<Role>> queryRoleByAccountCode(AccountRoleQuery accountRoleQuery) {
        SelectUserRoleReqVO selectUserRoleReqVO = new SelectUserRoleReqVO();
        selectUserRoleReqVO.setUserId(Long.valueOf(accountRoleQuery.getAccountCode()));
        BaseResult<List<UserRoleInfoResVO>> listBaseResult = roleFeignService.qryRoleListByUserId(selectUserRoleReqVO);
        List<UserRoleInfoResVO> listResultResponse = listBaseResult.getData();
        if (Objects.isNull(listResultResponse)){
            return ResultResponse.succResult();
        }
        List<Role> collect = listResultResponse.stream().map(tmp -> {
            Role role = new Role();
            role.setCode(String.valueOf(tmp.getRoleId()));
            role.setName(tmp.getRoleName());
            role.setDisplayName(tmp.getRoleName());
            return role;
        }).collect(Collectors.toList());
        return ResultResponse.succResult(collect);
    }

    @Override
    public ResultResponse<Boolean> hasRole(@NotNull AccountRoleQuery accountRoleQuery) {
        String roleCode = accountRoleQuery.getRoleCode();
        AccountRoleQuery roleQuery = new AccountRoleQuery();
        roleQuery.setAccountCode(SessionUtils.getUserCode());
        ResultResponse<List<Role>> listResultResponse = this.queryRoleByAccountCode(roleQuery);
        if (StringUtils.isNotEmpty(roleCode)) {
            List<Role> data = listResultResponse.getData();
            for (Role datum : data) {
                if (roleCode.equalsIgnoreCase(datum.getCode())) {
                    return ResultResponse.succResult(true);
                }
            }
        }
        return ResultResponse.succResult(false);

    }

    @Override
    public ResultResponse<Boolean> hasPermission(AccountPermissionQuery accountPermissionQuery) {
        return null;
    }

    @Override
    public ResultResponse<List<Permission>> getPermissionByAccountCode(String s) {
        return null;
    }

    @Override
    public ResultResponse<List<Role>> getRoleList() {
        BaseResult<List<RoleInfoResVO>> listBaseResult = roleFeignService.qryRoleList();
        List<RoleInfoResVO> listResultResponse = listBaseResult.getData();
        if (Objects.isNull(listResultResponse)){
            return ResultResponse.succResult();
        }
        List<Role> collect = listResultResponse.stream().map(tmp -> {
            Role role = new Role();
            role.setCode(String.valueOf(tmp.getRoleId()));
            role.setName(tmp.getRoleName());
            role.setDisplayName(tmp.getRoleName());
            return role;
        }).collect(Collectors.toList());
        return ResultResponse.succResult(collect);
    }

}
